package net.keebook.webapp.bean;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.AccountConfirmationDao;
import net.keebook.webapp.dao.SettingDao;
import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.entity.User.LoginMethod;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccountConfirmationMail;
import net.keebook.webapp.util.BCrypt;
import net.keebook.webapp.util.SettingId;

import org.apache.log4j.Logger;


@ManagedBean
@RequestScoped
public class SignupBean {
	private String name;
	private String email;
	private String password;
	private Boolean passwordChecked;
	private Boolean termsAccepted;
	@Inject
	private UserDao userDao;
	@Inject
	private SettingDao settingDao;
	@Inject
	private AccountConfirmationDao accountConfirmationDao;
	private SessionModel sessionModel;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		if (getSessionModel().getLoggedIn())  {
			FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
			return;
		}
	}
	
	public void signup() throws IOException {
		String systemTimeZoneId = getSettingDao().get(SettingId.SYSTEM_TIMEZONE).getValue();
		
		if (getSessionModel().getLoggedIn())  {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		User user = new User();
		user.setAdmin(false);
		user.setName(getName());
		user.setTermsAccepted(getTermsAccepted());
		user.setEmail(getEmail());
		user.setUsername(getEmail());
		user.setHashedPassword(BCrypt.hashpw(getPassword(), BCrypt.gensalt()));
		user.setTimezone(systemTimeZoneId);
		user.setLoginMethod(LoginMethod.PASSWORD);
		getUserDao().store(user);
		
		getSessionModel().clear();
		getSessionModel().setUserId(user.getId());
		
		AccountConfirmationMail.send(user, getAccountConfirmationDao(), getSettingDao());
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public SettingDao getSettingDao() {
		return settingDao;
	}

	public void setSettingDao(SettingDao settingDao) {
		this.settingDao = settingDao;
	}

	public AccountConfirmationDao getAccountConfirmationDao() {
		return accountConfirmationDao;
	}

	public void setAccountConfirmationDao(AccountConfirmationDao accountConfirmationDao) {
		this.accountConfirmationDao = accountConfirmationDao;
	}

	public Boolean getPasswordChecked() {
		return passwordChecked;
	}

	public void setPasswordChecked(Boolean passwordChecked) {
		this.passwordChecked = passwordChecked;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getTermsAccepted() {
		return termsAccepted;
	}

	public void setTermsAccepted(Boolean termsAccepted) {
		this.termsAccepted = termsAccepted;
	}
}
